import { ApiHideProperty, ApiProperty } from '@nestjs/swagger'
import { Column, Entity, JoinColumn, JoinTable, ManyToMany, ManyToOne } from 'typeorm'
import { SoftCompleteEntity } from '~/common/entity/common.entity'
import { MainPlanEntity } from '../main-plan/main-plan.entity'
import { PlanTaskEntity } from '../plan-task/plan-task.entity'

@Entity({ name: 'c_sub_plan' })
export class SubPlanEntity extends SoftCompleteEntity {
  @Column({ name: 'plan_id', comment: '主计划ID' })
  @ApiProperty({ description: '主计划ID' })
  planId: number

  @ApiHideProperty()
  @ManyToOne(() => MainPlanEntity, mainPlan => mainPlan.subPlans)
  @JoinColumn({ name: 'plan_id' })
  mainPlan: MainPlanEntity

  @Column({ name: 'sub_plan_id', comment: '子计划ID, SPL+yymmdd+000', unique: true })
  @ApiProperty({ description: '子计划ID, SPL+yymmdd+000' })
  subPlanId: string

  @Column({ name: 'sub_plan_name', length: 100, comment: '子计划名称' })
  @ApiProperty({ description: '子计划名称' })
  subPlanName: string

  @Column({ name: 'sub_plan_mark', length: 100, comment: '子计划备注' })
  @ApiProperty({ description: '子计划备注' })
  subPlanMark: string

  @ApiHideProperty()
  @ManyToMany(() => PlanTaskEntity, planTask => planTask.subPlans)
  @JoinTable({
    name: 'c_sub_plan_task',
    joinColumn: { name: 'sub_plan_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'task_id', referencedColumnName: 'id' },
  })
  tasks: PlanTaskEntity[]
}
